//-----------------------------------------------------------------------
// <copyright file="PresentDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BirthdayReminder.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace BirthdayReminder.Data
{    
    public abstract class PresentDAOBase
    {
        #region Common methods
        public virtual Present CreatePresentFromReader(IDataReader reader)
        {
            Present item = new Present();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("PresentId"))) item.PresentId = (int)reader["PresentId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Amount"))) item.Amount = (int)reader["Amount"];
                if (!reader.IsDBNull(reader.GetOrdinal("Type"))) item.Type = (int)reader["Type"];
                if (!reader.IsDBNull(reader.GetOrdinal("Message"))) item.Message = (string)reader["Message"];
                if (!reader.IsDBNull(reader.GetOrdinal("SenderId"))) item.SenderId = (int)reader["SenderId"];
                if (!reader.IsDBNull(reader.GetOrdinal("ReceiverId"))) item.ReceiverId = (int)reader["ReceiverId"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreationDate"))) item.CreationDate = (DateTime)reader["CreationDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("GiftId"))) item.GiftId = (int)reader["GiftId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreatePresentFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreatePresent methods
            
        public virtual void CreatePresent(Present present)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsInsert");
                
                database.AddInParameter(dbCommand, "@Amount", DbType.Int32, present.Amount);
                database.AddInParameter(dbCommand, "@Type", DbType.Int32, present.Type);
                database.AddInParameter(dbCommand, "@Message", DbType.String, present.Message);
                database.AddInParameter(dbCommand, "@SenderId", DbType.Int32, present.SenderId);
                database.AddInParameter(dbCommand, "@ReceiverId", DbType.Int32, present.ReceiverId);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, present.CreationDate);
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int32, present.GiftId);
                database.AddOutParameter(dbCommand, "@PresentId", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                present.PresentId = (int)database.GetParameterValue(dbCommand, "@PresentId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreatePresentException, ex);
            }
        }

        #endregion
		
        #region UpdatePresent methods
        
        public virtual void UpdatePresent(Present present)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsUpdate");            
                
                database.AddInParameter(dbCommand, "@PresentId", DbType.Int32, present.PresentId);
                database.AddInParameter(dbCommand, "@Amount", DbType.Int32, present.Amount);
                database.AddInParameter(dbCommand, "@Type", DbType.Int32, present.Type);
                database.AddInParameter(dbCommand, "@Message", DbType.String, present.Message);
                database.AddInParameter(dbCommand, "@SenderId", DbType.Int32, present.SenderId);
                database.AddInParameter(dbCommand, "@ReceiverId", DbType.Int32, present.ReceiverId);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, present.CreationDate);
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int32, present.GiftId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdatePresentException, ex);
            }
        }
        
        #endregion
		
        #region DeletePresent methods
        public virtual void DeletePresent(int presentId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsDelete");
                
                database.AddInParameter(dbCommand, "@PresentId", DbType.Int32, presentId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeletePresentException, ex);
            }
        }

        #endregion
		
        #region GetPresent methods
        
        public virtual Present GetPresent(int presentId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsGet");
                
                database.AddInParameter(dbCommand, "@PresentId", DbType.Int32, presentId);
                
                Present present =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        present = CreatePresentFromReader(reader);
                        reader.Close();
                    }
                }
                return present;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetPresentException, ex);
            }
        }
        
        #endregion
		
        #region GetPresentList methods
        public virtual PresentCollection GetPresentList(PresentColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PresentCollection presentCollection = new PresentCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Present present = CreatePresentFromReader(reader);
                        presentCollection.Add(present);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return presentCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetPresentListException, ex);
            }
        }
        
        public virtual PresentCollection GetPresentList(PresentColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetPresentList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		#region GetPresentsListByReceiverId methods
		public virtual PresentCollection GetPresentsListByReceiverId(int receiverId, PresentColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetPresentsListByReceiverId(receiverId , orderBy, orderDirection, 0, 0, out totalRecords);
        }
		
		public virtual PresentCollection GetPresentsListByReceiverId(int receiverId, PresentColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spPresentsGetListByReceiverId");
                
                database.AddInParameter(dbCommand, "@ReceiverId", DbType.Int32, receiverId);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                PresentCollection presentCollection = new PresentCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Present present = CreatePresentFromReader(reader);
                        presentCollection.Add(present);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return presentCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetPresentsListByReceiverIdException, ex);
            }
        }
		#endregion
		
    }
}